package com.android.cherryboard.Task;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.cherryboard.Utility.Utility;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class AddCherryboardTask extends AsyncTask<String, Void, String> {

	private Context context;
	private View view;
	private ProgressDialog progress;

	public AddCherryboardTask(View v) {
		// TODO Auto-generated constructor stub
		this.context = v.getContext();
		this.view = v;
	}

	@Override
	protected String doInBackground(String... params) {
		String url = "http://cherryfull.com/app_services.php?type=add_cherryboard&resolution_title="
				+ params[0] + "&fb_id=" + Utility.userUID;
		// TODO Auto-generated method stub
		JSONArray jsonArray = getJSONArrayFromURL(url);
		String result;
		try {
			result = jsonArray.getString(0);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			result = null;
			e.printStackTrace();
		}
		return result;
	}

	@Override
	protected void onPostExecute(String result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);
		progress.dismiss();
		Intent intent = ((Activity) context).getIntent();
		if (result != null) {
			Toast.makeText(context, result, Toast.LENGTH_LONG).show();
			if (result.equalsIgnoreCase("Exist")) {

				Toast.makeText(context, "Opps!!! This cherryboard already added", Toast.LENGTH_LONG).show();
 			}

			if (result.equalsIgnoreCase("Inserted")) {
				((Activity) context).setResult(Activity.RESULT_OK, intent);
				((Activity) context).finish();
			}
		} else {
			Toast.makeText(context, "Not Added", Toast.LENGTH_LONG).show();
			((Activity) context).setResult(Activity.RESULT_CANCELED, intent);

		}

	}

	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		super.onPreExecute();
		 progress = ProgressDialog.show(context, 
	                null, "Please Wait...");
	}

	private JSONArray getJSONArrayFromURL(String url) {
		InputStream mIs = null;
		String result = "";
		JSONArray jArrayEvent = null;
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(url);
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			mIs = entity.getContent();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			BufferedReader bufferReader = new BufferedReader(
					new InputStreamReader(mIs, "iso-8859-1"), 8);
			StringBuilder stringBuilder = new StringBuilder();
			String line = null;
			while ((line = bufferReader.readLine()) != null) {
				if (line.trim().equals("\n"))
					continue;
				stringBuilder.append(line + "\n");
			}
			mIs.close();
			result = stringBuilder.toString();
		} catch (Exception e) {

			Log.e("log_tag", "Error converting result " + e.toString());
		}
		try {
			JSONObject jsonObject = new JSONObject(result);
			jArrayEvent = jsonObject.getJSONArray("data");
			jArrayEvent = new JSONArray(result);
		} catch (JSONException e) {

			e.printStackTrace();
		}

		return jArrayEvent;
	}
}
